/**
 * 用于博客相關操作處理 
 * 
 */
import axios from 'axios'
import { observable } from 'mobx'
import store from 'store'
export default class PostService {
    @observable posts = [];  // 定义监控
    @observable semsg = ''; // 用於顯示博客提交是否成功處理 
    @observable famsg = ''; // 用於顯示博客提交是否成功處理
    @observable pagination = { page: 1, size: 20, pages: 1, count: 0 }; // 初始化定义页面 
    @observable post = '';

    pub(title, content) {
        axios.post('/api/post/pub', {
            title, content
        }, { headers: { 'jwt': store.get('token') } }
        ).then((response) => {
            this.semsg = "添加成功"
        }).catch(error => {
            this.famsg = "添加失敗"
        })
    }
    getall(search) {
        axios.get('/api/post/' + search).then((response) => {
            this.posts = response.data.posts; //触发变化
            this.pagination = response.data.pattern;  //触发变化 
        })
    }
    get(id) {
        axios.get('/api/post/' + id).then((response) => {
            this.post = response.data.post;
        }).catch(error => {
            console.log(error.data)
        })
    }
}
